#pragma once
#include <iostream>
#include <sys/time.h>
#include <sys/stat.h>
#include <fstream>
#include <atomic>
#include <boost/algorithm/string.hpp>
namespace lh_util
{
    static const std::string tmp_path = "./tmp/";
    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval tval;
            gettimeofday(&tval, nullptr);
            return std::to_string(tval.tv_sec);
        }
        static std::string GetTimeMc()
        {
            struct timeval tval;
            gettimeofday(&tval, nullptr);
            return std::to_string(tval.tv_usec);
        }
    };
    class PathUtil
    {
    public:
        static std::string AddSuff(const std::string &file_name, const std::string &suf)
        {
            std::string res;
            res = tmp_path + file_name + suf;
            return res;
        }
        static std::string Src(const std::string &file_name)
        {
            return AddSuff(file_name, ".cpp");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuff(file_name, ".exe");
        }
        static std::string CompileErr(const std::string &file_name)
        {
            return AddSuff(file_name, ".comerr");
        }
        static std::string In(const std::string &file_name)
        {
            return AddSuff(file_name, ".in");
        }
        static std::string Out(const std::string &file_name)
        {
            return AddSuff(file_name, ".out");
        }
        static std::string Err(const std::string &file_name)
        {
            return AddSuff(file_name, ".err");
        }
        static std::string GetUniqueName()
        {
            static std::atomic<int> id(0);
            // 获取毫秒级时间戳
            std::string time = TimeUtil::GetTimeMc();
            return time + "_" + std::to_string(++id);
        }
    };
    class FileUtil
    {
    public:
        static bool IsExist(const std::string &file_name)
        {
            struct stat st;
            if (stat(file_name.c_str(), &st) == 0)
                return true;
            return false;
        }
        static bool WriteFile(const std::string &file_name, const std::string code)
        {
            // 写文件
            std::ofstream out(file_name);
            if (!out.is_open())
                return false;
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }
        static bool ReadFile(const std::string &file_name, std::string *str, bool keep = false)
        {
            // 读取文件
            std::ifstream in(file_name);
            if (!in.is_open())
                return false;
            std::string buff;
            while (getline(in, buff))
            {
                (*str) += buff;
                if (keep)
                    (*str) += "\n";
            }
            return true;
        }
    };
    class StringUtil
    {
    public:
        /*
         * str : 要进行分割的字符串
         * target : 目标字符串数组
         * sep : 分割符
         */
        static void Split(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}